package com.zky.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Component
@Slf4j
public class JWTutils {
    private final String jwtSecret="zhaokaiyang";
    public String createToken(String account){
        Map<String, Object> claims = new HashMap<String, Object>();
        claims.put("account", account);
        Date date = new Date();
        long timeLimit=24*60*60*7;
        new Date(timeLimit + date.getTime());
        System.out.println(date.getTime());
        JwtBuilder jwtBuilder = Jwts.builder().setClaims(claims)
                .signWith(SignatureAlgorithm
                        .HS256, jwtSecret).setExpiration(new Date(timeLimit + date.getTime()));
        return jwtBuilder.compact();
    }
    public String parseToken(String token){
        Claims body = Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody();
        return body.get("account").toString();
    }
}
